package com.knightboy.smartshoes.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.knightboy.smartshoes.R;
import com.knightboy.smartshoes.activity.TestActivity;
import com.knightboy.smartshoes.core.SharedData;
import com.knightboy.smartshoes.db.StepDao;
import com.knightboy.smartshoes.util.TimeUtil;

import java.util.ArrayList;

/**
 * Created by 29288 on 2015/9/17.
 */
public class StepFragment extends Fragment implements View.OnClickListener {
    private Context context;
    private BroadcastReceiver receiver;

    private BarChart stepBarchat;
    private BarData barData;
    private TextView calorie_tv,minute_tv,distance_tv,step_tv,debug_tv;
    private final String ACTION_DATA_UPDATE = "com.knightBoy.dataUpdate";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.fragment_step,container,false);
        context = this.getActivity();
        //获得控件
        calorie_tv = (TextView)rootview.findViewById(R.id.calorie_tv);
        minute_tv = (TextView)rootview.findViewById(R.id.minute_tv);
        distance_tv = (TextView)rootview.findViewById(R.id.distance_tv);
        step_tv = (TextView)rootview.findViewById(R.id.step_tv);
        debug_tv = (TextView)rootview.findViewById(R.id.debug_tv);
        debug_tv.setOnClickListener(this);
        stepBarchat = (BarChart)rootview.findViewById(R.id.step_barchart);

        initData();
        return rootview;
    }

    @Override
    public void onStart() {
        super.onStart();
        //柱形图显示
        barData = getBarData(24, 10);
        showBarChart(stepBarchat, barData);
        //注册接收数据更新的广播接收器
        initReceiver();
    }

    /**
     * 显示柱形图
     * @param barChart 柱形图对象
     * @param barData 柱形图数据
     */
    private void showBarChart(BarChart barChart, BarData barData) {
        barChart.setDrawBorders(false);  ////是否在折线图上添加边框

        barChart.setDescription("");// 数据描述

        // 如果没有数据的时候，会显示这个，类似ListView的EmptyView
        barChart.setNoDataTextDescription("You need to provide data for the chart.");

        barChart.setDrawGridBackground(false); // 是否显示表格颜色
        barChart.setGridBackgroundColor(Color.WHITE & 0x70FFFFFF); // 表格的的颜色，在这里是是给颜色设置一个透明度

        barChart.setTouchEnabled(true); // 设置是否可以触摸

        barChart.setDragEnabled(true);// 是否可以拖拽
        barChart.setScaleEnabled(true);// 是否可以缩放

        barChart.setPinchZoom(false);//

        barChart.setBackgroundColor(getResources().getColor(R.color.white));// 设置背景

        barChart.setDrawBarShadow(true);

        barChart.setData(barData); // 设置数据

        Legend mLegend = barChart.getLegend(); // 设置比例图标示

        mLegend.setForm(Legend.LegendForm.CIRCLE);// 样式
        mLegend.setFormSize(6f);// 字体
        mLegend.setTextColor(Color.BLACK);// 颜色

        barChart.animateX(1000); // 立即执行的动画,x轴
    }

    /**
     * 获得柱形图数据
     * @param count 数据个数
     * @param range 数据最大值
     * @return
     */
    private BarData getBarData(int count, float range) {
        ArrayList<String> xValues = new ArrayList<String>();
        for (int i = 0; i < count; i++) {
            xValues.add(i+1 + ":00");
        }

        ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();
        for(int i = 0; i < 9; i++){
            yValues.add(new BarEntry(0,0));
        }
        yValues.add(new BarEntry((float)0.2,9));
        yValues.add(new BarEntry((float)0.3,10));
        yValues.add(new BarEntry((float)0.4,11));
        yValues.add(new BarEntry((float)0.2,12));
        yValues.add(new BarEntry((float)0.1,13));
        yValues.add(new BarEntry((float) 0.2, 14));
        for(int i = 15; i < 24; i++){
            yValues.add(new BarEntry(0,0));
        }
//        StepDao stepDao = new StepDao(context);
//        int[] steps = stepDao.querySteps(TimeUtil.getNowDay());
//        for(int i = 0; i < count; i++){
//            float value = (float)steps[i];
//            yValues.add(new BarEntry(value, i));
//        }
//        for (int i = 0; i < count; i++) {
//            float value = (float) (Math.random() * range/*100以内的随机数*/) + 3;
//            yValues.add(new BarEntry(value, i));
//        }

        // y轴的数据集合
        BarDataSet barDataSet = new BarDataSet(yValues, "行走步数分布图(单位:百步)");

        barDataSet.setColor(Color.rgb(27, 155, 244));

        ArrayList<BarDataSet> barDataSets = new ArrayList<BarDataSet>();
        barDataSets.add(barDataSet); // add the datasets

        BarData barData = new BarData(xValues, barDataSets);

        return barData;
    }

    /**
     * 初始化数据更新的广播接收器
     */
     private void initReceiver(){
         receiver = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
                 //更新走路数据
                 //更新卡路里数据
                 calorie_tv.setText((SharedData.getActiveCalorie() + SharedData.getRestCalorie()) + "" );
                 //更新走路时间
                 minute_tv.setText(SharedData.getActiveTime() + "m");
                 //更新路程
                 distance_tv.setText((SharedData.getDistance()/1000 + "").substring(0,3));
                 //更新步数
                 step_tv.setText((SharedData.getStep())+"");
             }
         };
         //注册接收数据更新的广播接收器
         IntentFilter intentFilter = new IntentFilter(ACTION_DATA_UPDATE);
         context.registerReceiver(receiver,intentFilter);
     }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.debug_tv:
                context.startActivity(new Intent(context, TestActivity.class));
                break;
        }
    }

    private void initData(){
        //更新走路数据
        //更新卡路里数据
        calorie_tv.setText((SharedData.getActiveCalorie() + SharedData.getRestCalorie()) + "" );
        //更新走路时间
        minute_tv.setText(SharedData.getActiveTime() + "m");
        //更新路程
        distance_tv.setText((SharedData.getDistance()/1000 + "").substring(0,3));
    }
}
